package net.cyue.homework.student.management.servlet.student;

import jakarta.servlet.annotation.WebServlet;
import jakarta.validation.Valid;
import net.cyue.homework.student.management.dto.DeleteDTO;
import net.cyue.homework.student.management.dto.MajorAddDTO;
import net.cyue.homework.student.management.dto.MajorUpdateDTO;
import net.cyue.homework.student.management.entity.Major;
import net.cyue.homework.student.management.http.HTTPServlet;
import net.cyue.homework.student.management.http.WebResult;
import net.cyue.homework.student.management.http.annotation.DeleteMethod;
import net.cyue.homework.student.management.http.annotation.PostMethod;
import net.cyue.homework.student.management.http.annotation.PutMethod;
import net.cyue.homework.student.management.util.DB;
import net.cyue.nest.nestjava.common.annotation.Body;
import net.cyue.nest.nestjava.common.annotation.Response;
import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPRequest;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(
    name = "MajorServlet",
    value = {
        "/api/major",
        "/api/major/"
    }
)
public class MajorServlet extends HTTPServlet {

    @Override
    public void doGet(
        IHTTPRequest request,
        IHTTPResponse response
    ) throws IOException {
        String majorID = request.getParameter("id");
        if (majorID == null || majorID.isEmpty()) {
            response.send(WebResult.failed("Major ID is required"));
            return;
        }

        try {
            ResultSet resultSet = DB.POOL.query("SELECT * FROM major WHERE id =" + majorID + " LIMIT 1");
            if (!resultSet.next()) {
                resultSet.close();
                response.code(HTTPStatus.NOT_FOUND);
                response.send(WebResult.failed("专业不存在"));
                return;
            }
            Integer id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            resultSet.close();
            response.send(WebResult.success(
                "获取成功",
                new Major(id, name)
            ));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，获取失败"));
        }
    }

    @PostMethod
    public void addMajor(
        @Response IHTTPResponse response,
        @Valid @Body MajorAddDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionUpdate("INSERT INTO major(name) VALUES ('" + dto.getName() + "')");
            if (rows == 0) {
                response.send(WebResult.failed("添加失败"));
                return;
            }
            response.send(WebResult.success("添加成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，添加失败"));
        }
    }

    @PutMethod
    public void updateMajor(
        @Response IHTTPResponse response,
        @Valid @Body MajorUpdateDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionUpdate("update major set name = '" + dto.getName() + "' where id = " + dto.getId());
            if (rows == 0) {
                response.send(WebResult.failed("更新失败"));
                return;
            }
            response.send(WebResult.success("更新成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，更新失败"));
        }
    }

    @DeleteMethod
    public void deleteMajor(
        @Response IHTTPResponse response,
        @Valid @Body DeleteDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionUpdate("delete from major where id = " + dto.getId());
            if (rows == 0) {
                response.send(WebResult.failed("删除失败"));
                return;
            }
            response.send(WebResult.success("删除成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，删除失败"));
        }
    }
}

